package util;

import domain.User;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;


public class DbUtil {
    public static void main(String[] args) throws Exception {
        //注册驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //创建连接
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/eesy?useUnicode=true&useSSL=false&characterEncoding=utf-8&serverTimezone=GMT","root", "1234");
        //创建preparedStatement对象
        PreparedStatement statement = connection.prepareStatement("select * from user");
        //查询
        ResultSet resultSet = statement.executeQuery();
        //操作结果集
        List<User> users = new ArrayList<>();
        while (resultSet.next()){
            User user = new User();
            user.setId(resultSet.getInt("id"));
            user.setUsername(resultSet.getString("username"));
            user.setAddress(resultSet.getString("address"));
            user.setSex(resultSet.getString("sex"));
            user.setBirthday(resultSet.getDate("birthday"));
            users.add(user);
        }
        users.forEach(System.out::println);
        resultSet.close();
        statement.close();
        connection.close();
    }
}
